/**
 * @Copyright:Copyright (c) 1992 - 2015
 * @Company: TengTong
 */
package com.tengtong.duang.commons.convert.support;

import com.tengtong.duang.commons.convert.ConditionalConverter;
import com.tengtong.duang.commons.convert.ConversionService;
import com.tengtong.duang.commons.utils.ReflectUtils;

/**
 * Array 转成数字。 在转换的时候是有限制的，目前只支持单个字符串的  String[] 数组转成数字
 *
 * @author jai
 * @since: v1.0
 */
public class ArrayToNumberConverter implements ConditionalConverter{

	private final ArrayToStringConverter helperConverter;
	
	public ArrayToNumberConverter(ConversionService helperConverter){
		this.helperConverter = new ArrayToStringConverter(helperConverter);
	}
	
	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.convert.ConditionalConverter#convert(java.lang.Object, java.lang.Class)
	 */
	@Override
	public Object convert(Object source, Class<?> targetType) {
		//先将数组转为字符串
		Object value = this.helperConverter.convert(source, String.class);
		
		StringToNumberConverter factory = new StringToNumberConverter();
		return factory.convert(value, targetType);
	}

	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.convert.ConditionalConverter#matches(java.lang.Class, java.lang.Class)
	 */
	@Override
	public boolean matches(Class<?> sourceType, Class<?> targetType) {
		return sourceType.isArray() && ReflectUtils.isInheritance(targetType, Number.class);
	}

}
